package Ep01_Array.Feb28th_0704;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/1 10:40
 * 说明：0704 二分查找 （int[] has been sorted）
 */

public class Mine {
    public static void main(String[] args) {
        int[] nums = {2, 5};
        Mine m = new Mine();
        System.out.println(m.search(nums, 5));
    }


    public int search(int[] nums, int target){
        int low = 0;
        int high = nums.length - 1;
        while(low<=high){
            int mid = (high + low) / 2;
            int num = nums[mid];
            if (num == target){
                return mid;
            } else if (num < target) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return -1;
    }
}
